<template>
	<view>
		<view class="cu-custom" :style="[{height:CustomBar + 'px'}]">
			<view class="cu-bar fixed" :style="style" :class="[bgImage!=''?'none-bg text-white bg-img':'',ThemeColor]">
				<view class="action" @tap="BackPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
					<view class="action" @tap.stop="leftIconAction" v-if="showLeftIcon">
						<text :class="'cuIcon-'+leftIcon+' padding-left-xl'"></text>
					</view>
				</view>
				<view v-else-if="leftMore">
					<view :style="[{top:StatusBar + 'px'}]" class="action" @tap="showExpandMenuAction($event)" v-if="showLeftIcon">
						<text class="cuIcon-more padding-left"></text>
					</view>
				</view>
				<view v-else>
					<view :style="[{top:StatusBar + 'px'}]" class="action" @tap="leftIconAction" v-if="showLeftIcon">
						<text :class="'cuIcon-'+leftIcon+' padding-left'"></text>
					</view>
				</view>
				<view v-if="navItems!==null" style="width: 100%;">
					<view class="action" :style="'width:100%;'+[{top:StatusBar + 'px;'}]">
						<view class="flex justify-center align-center">
							<view v-for="(item,index) in navItems" :key="index">
								<text @tap="tabSelect(index)" :class="'text-bold text-lg text-'+[TabCur==index?'orange':'white']+[index>0?' padding-left':'']">{{item}}</text>
							</view>
						</view>
					</view>
				</view>
				<view v-else-if="middleIcon&&middleIcon.length>0" class="action flex justify-center align-center" style="width: 40%;">
					<slot name="content"></slot>
					<text @tap.stop="middleIconClick" v-if="middleIcon&&middleIcon.length>0" :class="'action padding-left-xs text-white cuIcon-'+middleIcon"></text>
				</view>
				<view v-else class="content" :style="[{top:StatusBar + 'px'}]">
					<slot name="content"></slot>
				</view>
				<slot name="right"></slot>
			</view>
		</view>
		<chunLei-popups v-model="showExpandMenu" :x="x" :y="y" dynamic>
			<view :class="'flex align-center justify-start '+[index!==expandMenuList.length-1?'padding-bottom-sm':'']" style="flex-direction: column;" v-for="(item,index) in expandMenuList" :key="index">
				<view @tap="tapPopup(index)" class="flex justify-start align-center">
					<text :class="'padding-right-xs cuIcon-'+item.icon"></text>
					<text class="text-df">{{item.title}}</text>
				</view>
			</view>
		</chunLei-popups>
	</view>
</template>

<script>
	import config from '@/api/config.js'
	import {
		getAppSettingInfo
	} from '@/api/statusCache.js'
	export default {
		data() {
			return {
				x:30,
				y:50,
				showExpandMenu:false,
				expandMenuList:[
					{
						title:'立即搜索',
						icon:'search'
					},
					{
						title:'浏览模式',
						icon:'list'
					}
				],
				TabCur: 0,
				scrollLeft: 0,
				StatusBar: 0,
				CustomBar: 0,
				ThemeColor: this.ThemeColor
			};
		},
		name: 'cu-custom',
		computed: {
			style() {
				var StatusBar= this.StatusBar;
				var CustomBar= this.CustomBar;
				var bgImage = this.bgImage;
				var style = `height:${CustomBar}px;padding-top:${StatusBar}px;`;
				if (this.bgImage) {
					style = `${style}background-image:url(${bgImage});`;
				}
				return style
			}
		},
		props: {
			// #ifdef MP-BAIDU
			showLeftIcon: {
				type: Boolean,
				default: false
			},
			leftMore: {
				type: Boolean,
				default: false
			},
			isBack: {
				type: Boolean,
				default: false
			},
			isFromOutside:{
				type: Boolean,
				default: false
			},
			// #endif
			// #ifndef MP-BAIDU
			showLeftIcon: {
				type: [Boolean, String],
				default: false
			},
			leftMore: {
				type: [Boolean, String],
				default: false
			},
			isBack: {
				type: [Boolean, String],
				default: false
			},
			isFromOutside:{
				type: [Boolean, String],
				default: false
			},
			// #endif
			bgColor: {
				type: String,
				default: ''
			},
			bgImage: {
				type: String,
				default: ''
			},
			leftIcon: {
				type: String,
				default: 'search'
			},
			navItems: {
				type: Array,
				default: null
			},
			middleIcon: {
				type: String,
				default: ''
			}
		},
		created() 
		{
			let _this = this
			uni.getSystemInfo({
				success(e) {
					_this.StatusBar = e.statusBarHeight;
					// #ifdef MP-WEIXIN || MP-QQ
					let custom = wx.getMenuButtonBoundingClientRect();
					_this.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif		
					
					// #ifdef MP-BAIDU
					let custom = swan.getMenuButtonBoundingClientRect();
					_this.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					// #endif	
							
					// #ifdef MP-ALIPAY
					_this.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
		},
		methods: {
			middleIconClick() 
			{
				this.$emit('middleIconClick')
			},
			tapPopup(index){
				this.$emit('tapMore',index)
			},
			showExpandMenuAction(e)
			{
				this.x = e.touches[0].clientX
				this.y = e.touches[0].clientY
				this.showExpandMenu = !this.showExpandMenu
			},
			tabSelect(index) {
				console.log('index:'+index)
				this.TabCur = index
				this.$emit('tabSelect',index)
			},
			leftIconAction() 
			{
				this.$emit('leftBtnClick')
			},
			BackPage() {
				//从外部唤起的直接就回首页
				if(this.isFromOutside) {
					uni.removeStorageSync('AppLaunchFromTimeline')
					uni.reLaunch({
						url:'/pages/index/index'
					})
					return
				}
				//返回前触发事件，可以用于通信回调
				this.$emit('beforeBack')
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>

<style>

</style>
